package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SkuInfo;
import com.atguigu.gmall.product.service.SkuManageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private SkuManageService skuManageService;


    /**
     * 保存商品SKU
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 分页查询商品SKU列表
     *
     * @param page        页码
     * @param limit       页大小
     * @param category3Id 分类ID
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuByPage(@PathVariable("page") long page, @PathVariable("limit") long limit, @RequestParam("category3Id") Long category3Id) {
        Page<SkuInfo> infoPage = new Page<>(page, limit);
        infoPage = skuManageService.getSkuByPage(infoPage, category3Id);
        return Result.ok(infoPage);
    }

    /**
     * 对商品SKU上架处理
     *
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuManageService.onSale(skuId);
        return Result.ok();
    }


    /**
     * 对商品SKU下架处理
     *
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }


    /**
     * 获取当前ID的sku属性值
     * @return
     */
    @GetMapping("/getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuManageService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 修改商品SKU所有销售属性包含属性值列表
     * @return
     */
    @PostMapping("/updateSkuInfo")
    public Result getSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.updateSkuInfo(skuInfo);
        return Result.ok();
    }

}